package org.luxor.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.luxor.commons.core.validator.group.AddGroup;
import org.luxor.commons.core.validator.group.UpdateGroup;
import org.luxor.commons.core.web.controller.BaseRestController;
import org.luxor.commons.core.web.data.R;
import org.luxor.commons.jdbc.metadata.PageQuery;
import org.luxor.system.entity.SysMenuEntity;
import org.luxor.system.entity.SysRoleMenuRefEntity;
import org.luxor.system.service.ISysMenuService;
import org.luxor.system.service.ISysRoleMenuRefService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.stream.Collectors;

/**
 * 角色与菜单对应关系
 *
 * @author Mark @date 2020-12-29 10:27:13
 */
@RestController
@RequestMapping("/system/sysRoleMenuRef")
@Api(tags = "角色与菜单对应关系")
public class SysRoleMenuRefController extends BaseRestController {

    @Resource
    private ISysRoleMenuRefService sysRoleMenuRefService;
    @Resource
    private ISysMenuService sysMenuService;

    @ApiOperation("分页列表")
    @PostMapping("/list")
    public R<IPage<SysRoleMenuRefEntity>> list(@RequestBody PageQuery<HashMap> searchPage) {
        IPage<SysRoleMenuRefEntity> data = sysRoleMenuRefService.page(searchPage.getPage(), searchPage.getSearch());
        return R.ok(data);
    }

    @ApiOperation("详情信息")
    @ApiImplicitParam(name = "id", value = "主键ID", paramType = "path", required = true)
    @GetMapping("/info/{id}")
    public R<SysRoleMenuRefEntity> info(@PathVariable("id") Long id) {
        SysRoleMenuRefEntity sysRoleMenuRef = sysRoleMenuRefService.getById(id);
        return R.ok(sysRoleMenuRef);
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public R<SysRoleMenuRefEntity> save(@Validated(AddGroup.class) @RequestBody SysRoleMenuRefEntity sysRoleMenuRef) {
        sysRoleMenuRefService.save(sysRoleMenuRef);
        return R.ok(sysRoleMenuRef);
    }

    @ApiOperation("修改")
    @PutMapping("/update")
    public R<SysRoleMenuRefEntity> update(@Validated(UpdateGroup.class) @RequestBody SysRoleMenuRefEntity sysRoleMenuRef) {
        sysRoleMenuRefService.updateById(sysRoleMenuRef);
        return R.ok(sysRoleMenuRef);
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        sysRoleMenuRefService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @ApiOperation("角色关联的菜单列表")
    @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "path", required = true)
    @GetMapping("/{roleId}/menus")
    public R<Collection<SysMenuEntity>> menus(@PathVariable("roleId") Long roleId) {
        Collection<SysRoleMenuRefEntity> roleMenuRefs = sysRoleMenuRefService.listByRoleId(roleId);
        Collection<Long> menuIds = roleMenuRefs.stream().map(SysRoleMenuRefEntity::getMenuId).collect(Collectors.toSet());
        Collection<SysMenuEntity> menus = sysMenuService.listByIds(menuIds);
        return R.ok(menus);
    }
}
